﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Device.Location;
using BankProject.Strings;
using Microsoft.Devices.Sensors;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using BankProject.Entities;

namespace BankProject.Models
{
    [DataContractAttribute]
    public class LocationManager : BaseModel
    {
        //[DataMemberAttribute]
        private GeoCoordinateWatcher geoCoordinateWatcher;
        private Compass compass;

        public void Start()
        {
            if (geoCoordinateWatcher != null)
            {
                geoCoordinateWatcher.Start();
            }
        }

        public void Stop()
        {
            if (geoCoordinateWatcher != null)
            {
                geoCoordinateWatcher.Stop();
            }
        }

        public void StartCompass()
        {
            if (compass != null)
            {
                compass.Start();
            }
        }

        public void StopCompass()
        {
            if (compass != null)
            {
                compass.Stop();
            }
        }

        public void InitializeLocationServices()
        {
            geoCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            geoCoordinateWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoCoordinateWatcher_PositionChanged);
            geoCoordinateWatcher.Start();

            if (Compass.IsSupported)
            {
                compass = new Compass();
                compass.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<CompassReading>>(compass_CurrentValueChanged);
            }
        }

        public LocationManager(bool isLocationEnabled)
        {
            InitializeLocationServices();
        }

        public void UnTombstoned()
        {
            InitializeLocationServices();
        }

        public void Deactivated()
        {
            if (geoCoordinateWatcher != null)
            {
                geoCoordinateWatcher.Stop();
            }
        }

        public void UnDormant()
        {
            geoCoordinateWatcher.Start();
        }

        private void geoCoordinateWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            GeoCoordinate Location = e.Position.Location;

            App.ViewModel.Location = Location;
        }

        void compass_CurrentValueChanged(object sender, SensorReadingEventArgs<CompassReading> e)
        {
            if (compass.IsDataValid)
            {
                if (App.ViewModel.Course.Equals(double.NaN))
                {
                    App.ViewModel.Course = e.SensorReading.TrueHeading;
                }
                else
                {
                    if (Math.Abs(App.ViewModel.Course - e.SensorReading.TrueHeading) > 180)
                    {
                        App.ViewModel.Course = App.ViewModel.Course * 0.8 + ((e.SensorReading.TrueHeading > 180) ? (e.SensorReading.TrueHeading - 360) : (e.SensorReading.TrueHeading + 360)) * 0.2;
                    }
                    else
                    {
                        App.ViewModel.Course = App.ViewModel.Course * 0.8 + e.SensorReading.TrueHeading * 0.2;
                    }
                }
            }
        }
    }
}
